package com.itheima.api.sms;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.sms.SmsFlashPromotionSessionMapper;
import com.itheima.vo.dto.SmsFlashPromotionSessionDto;
import com.itheima.vo.pojo.SmsFlashPromotionSession;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
public class SmsFlashPromotionSessionServiceImpl extends ServiceImpl<SmsFlashPromotionSessionMapper, SmsFlashPromotionSession> implements SmsFlashPromotionSessionService{

    @Autowired
    private SmsFlashPromotionSessionMapper smsFlashPromotionSessionMapper;
    @Autowired
    private SmsFlashPromotionProductRelationService smsFlashPromotionProductRelationService;

    //修改场次的状态
    @Override
    public void updateStatus(SmsFlashPromotionSession smsFlashPromotionSession) {
        smsFlashPromotionSessionMapper.updateStatus(smsFlashPromotionSession);
    }

    //获取全部可选场次及其数量(用在秒杀活动列表里的设置商品)
    @Override
    public List<SmsFlashPromotionSessionDto> selectList(Long flashPromotionId) {
        List<SmsFlashPromotionSessionDto> result = new ArrayList<>();
        //从sms_flash_promotion_session表里获取到所有状态为1的活动
        LambdaQueryWrapper<SmsFlashPromotionSession> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmsFlashPromotionSession::getStatus,1);

        //遍历所有状态状态为1的活动,根据两个id找到对相应商品数量
        List<SmsFlashPromotionSession> list = smsFlashPromotionSessionMapper.selectList(queryWrapper);
        for (SmsFlashPromotionSession promotionSession : list) {
            SmsFlashPromotionSessionDto dto = new SmsFlashPromotionSessionDto();
            //数据拷贝
            BeanUtils.copyProperties(promotionSession, dto);
            //得到对应的商品数量
            long count = smsFlashPromotionProductRelationService.getCount(flashPromotionId, promotionSession.getId());
            dto.setProductCount(count);
            result.add(dto);
        }
        return result;
    }

}
